package com.yzt.act.controller;

import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.yzt.act.entity.Evection;
import com.yzt.act.kit.SearchKit;
import com.yzt.act.service.IEvectionService;
import com.yzt.base.entity.BaseUser;
import com.yzt.base.kit.Base;
import com.yzt.base.utils.SecurityUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 事件概述表 前端控制器
 * </p>
 *
 * @author Y
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/act/evection")
@Api("")
public class EvectionController {

	@Autowired
	private TaskService taskService;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private IEvectionService evectionService;

	@PostMapping("startEvection")
	@ApiOperation("开始流程")
	@Transactional(rollbackFor = Exception.class)
	public Base<Object> startEvection(Evection e, String processKey) throws Exception {
		BaseUser user = SecurityUtils.getUser();
		Map<String, Object> v = new HashMap<>();
		v.put("assignee", user.getUsername());
		e.setUserId(user.getId());
		e.setStatus("0");
		boolean save = evectionService.save(e);
		if (!save) {
			throw new Exception("流程开始失败");
		}
		v.put("evection", e);
		ProcessInstance instance = runtimeService.startProcessInstanceByKey(processKey, e.getId() + "", v);
		if (instance == null) {
			throw new Exception("流程开始失败");
		}
		Task task = taskService.createTaskQuery().processDefinitionKey(processKey).taskAssignee(user.getUsername())
				.singleResult();
		if (task != null) {
			taskService.complete(task.getId());
		} else {
			throw new Exception("流程开始失败");
		}
		return Base.ok();
	}

	@GetMapping("getSelfEvection")
	@ApiOperation("查询自己所有事件")
	public Base<Object> getSelfEvection(SearchKit kit) {
		BaseUser user = SecurityUtils.getUser();
		kit.setUserId(user.getId() + "");
		PageInfo<Evection> info = evectionService.getEvectionList(kit);
		return Base.okData(info);
	}

	@GetMapping("getSelfEvectionProcess")
	@ApiOperation("查询自己待处理的流程事件")
	public Base<Object> getSelfEvectionProcess(SearchKit kit) {
		PageInfo<Evection> info = evectionService.getEvectionProcessList(kit);
		return Base.okData(info);
	}

	@PostMapping("completeEvection/{eid}")
	@ApiOperation("流程审核通过")
	public Base<Object> completeEvection(@PathVariable String eid) {
		boolean b = evectionService.completeEvection(eid);
		if (b)
			return Base.ok("审核通过");
		return Base.fail("审核已通过");
	}

	@PostMapping("deleteEvection/{eid}")
	@ApiOperation("删除事件流程")
	public Base<Object> deleteEvection(@PathVariable String eid) {
		boolean b = evectionService.deleteEvection(eid);
		if (b)
			return Base.ok("删除成功");
		return Base.fail();
	}

}
